﻿using MathTutorPro.DAL;
using MathTutorPro.Data.Entities;
using Microsoft.EntityFrameworkCore;

namespace MathTutorPro.Data.DAL
{
    /// <summary>
    /// 群组成员数据访问层
    /// </summary>
    public class GroupChatMemberDal
    {
        private readonly AppDbContext _context;

        public GroupChatMemberDal(AppDbContext context)
        {
            _context = context;
        }

        /// <summary>
        /// 获取群组成员列表
        /// </summary>
        public async Task<List<ChatGroupMember>> GetGroupMembersAsync(int groupId)
        {
            return await _context.ChatGroupMember
                .Include(m => m.User)
                .Where(m => m.ChatGroupId == groupId && !m.IsDelete)
                .OrderBy(m => m.JoinTime)
                .ToListAsync();
        }

        /// <summary>
        /// 添加群组成员
        /// </summary>
        public async Task AddMemberAsync(ChatGroupMember member)
        {
            _context.ChatGroupMember.Add(member);
            await _context.SaveChangesAsync();
        }

        /// <summary>
        /// 移除群组成员
        /// </summary>
        public async Task RemoveMemberAsync(int groupId, string userId)
        {
            var member = await _context.ChatGroupMember
                .FirstOrDefaultAsync(m => m.ChatGroupId == groupId && m.UserId == userId && !m.IsDelete);

            if (member != null)
            {
                member.IsDelete = true;
                await _context.SaveChangesAsync();
            }
        }

        /// <summary>
        /// 检查用户是否是群组成员
        /// </summary>
        public async Task<bool> IsMemberAsync(int groupId, string userId)
        {
            return await _context.ChatGroupMember
                .AnyAsync(m => m.ChatGroupId == groupId && m.UserId == userId && !m.IsDelete);
        }

        /// <summary>
        /// 获取群组成员数量
        /// </summary>
        public async Task<int> GetMemberCountAsync(int groupId)
        {
            return await _context.ChatGroupMember
                .CountAsync(m => m.ChatGroupId == groupId && !m.IsDelete);
        }

        /// <summary>
        /// 批量添加群组成员
        /// </summary>
        public async Task AddMembersAsync(IEnumerable<ChatGroupMember> members)
        {
            await _context.ChatGroupMember.AddRangeAsync(members);
            await _context.SaveChangesAsync();
        }

        /// <summary>
        /// 获取用户在指定群组中的角色
        /// </summary>
        public async Task<bool> IsCreatorAsync(int groupId, string userId)
        {
            var group = await _context.ChatGroup
                .FirstOrDefaultAsync(g => g.Id == groupId && !g.IsDelete);

            return group != null && group.CreateUserId == userId;
        }

        /// <summary>
        /// 获取用户加入的所有群组ID
        /// </summary>
        public async Task<IEnumerable<int>> GetUserGroupIdsAsync(string userId)
        {
            return await _context.ChatGroupMember
                .Where(m => m.UserId == userId && !m.IsDelete)
                .Select(m => m.ChatGroupId)
                .ToListAsync();
        }

        /// <summary>
        /// 获取群组中所有成员的ID
        /// </summary>
        public async Task<IEnumerable<string>> GetGroupMemberIdsAsync(int groupId)
        {
            return await _context.ChatGroupMember
                .Where(m => m.ChatGroupId == groupId && !m.IsDelete)
                .Select(m => m.UserId)
                .ToListAsync();
        }

        /// <summary>
        /// 获取群组中所有成员（包括用户信息）
        /// </summary>
        public async Task<IEnumerable<ChatGroupMember>> GetGroupMembersWithDetailsAsync(int groupId)
        {
            return await _context.ChatGroupMember
                .Include(m => m.User)
                .Where(m => m.ChatGroupId == groupId && !m.IsDelete)
                .OrderBy(m => m.JoinTime)
                .ToListAsync();
        }
    }
}
